<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:ohrm="http://esrc.unimelb.edu.au/OHRM#"
   xmlns:rdfs="http://www.w3.org/TR/WD-rdf-schema#"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
   xmlns:fn="http://www.w3.org/2005/xpath-functions"
   xmlns="http://ands.org.au/standards/rif-cs/registryObjects"
   exclude-result-prefixes="xsl xsd rdfs rdf ohrm fn">

   <!-- author: Cyrus Keong-->
   <xsl:output method="xml" encoding="UTF-8" indent="yes"
      omit-xml-declaration="yes"/>
   <xsl:variable name="groupDescription" select="'YRC_OHRM'"/>
   <xsl:variable name="originatingSource" select="'unimelb-OHRM'"/>

   <xsl:variable name="rifcsDoc" select="document('rifcs-mapping.xml')/dataroot"/>

   <xsl:variable name="select_all_OHRM_Relationships">
      <xsl:copy-of
         select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='RELATEDENTITY' and OHRM_TABLE_COLUMN='TYPE' ]"
      />
   </xsl:variable>

   <xsl:variable name="select_all_OHRM_EntityName">
      <xsl:copy-of
         select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='ENTITYNAME' and OHRM_TABLE_COLUMN='ENALTERNATE' ]"
      />
   </xsl:variable>

   <!-- string for default namespace uri and schema location -->
   <xsl:variable name="ns"
      select="'http://ands.org.au/standards/rif-cs/registryObjects'"/>
   <xsl:variable name="schemaLoc"
      select="'http://ands.org.au/standards/rif-cs/registryObjects http://services.ands.org.au/documentation/rifcs/schema/registryObjects.xsd'"/>


   <xsl:template match="rdf:RDF">

      <xsl:for-each select="/*">


         <xsl:element name="registryObjects" namespace="{$ns}">
            <xsl:attribute name="xsi:schemaLocation">

               <xsl:value-of select="$schemaLoc"/>
            </xsl:attribute>

            <xsl:for-each select="rdf:Description">

               <xsl:variable name="ohrmEntity">
                  <xsl:value-of
                     select="rdf:type[@rdf:resource!='OHRM:ENTITY']/attribute::node()"
                  />
               </xsl:variable>

               <xsl:variable name="RIFCS_Entity_Sub_Type">
                  <xsl:call-template name="mapping_OHRM_EntitySubType_to_RIFCS">
                     <xsl:with-param name="ohrmEntitySubType">
                        <xsl:value-of select="$ohrmEntity"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </xsl:variable>

               <xsl:variable name="RIFCS_Entity_Type">
                  <xsl:call-template name="mapping_OHRM_EntityType_to_RIFCS">
                     <xsl:with-param name="ohrmEntityType">
                        <xsl:value-of select="$ohrmEntity"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </xsl:variable>

               <xsl:if test="$RIFCS_Entity_Type='Activity'">
                  <xsl:call-template name="createRIFCSActivity">
                     <xsl:with-param name="activitytype">
                        <xsl:value-of select="$RIFCS_Entity_Sub_Type"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </xsl:if>
               <xsl:if test="$RIFCS_Entity_Type='Collection'">
                  <xsl:call-template name="createRIFCSCollection">
                     <xsl:with-param name="collectiontype">
                        <xsl:value-of select="$RIFCS_Entity_Sub_Type"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </xsl:if>
               <xsl:if test="$RIFCS_Entity_Type='Service'">
                  <xsl:call-template name="createRIFCSService">
                     <xsl:with-param name="servicetype">
                        <xsl:value-of select="$RIFCS_Entity_Sub_Type"/>
                     </xsl:with-param>

                  </xsl:call-template>
               </xsl:if>
               <xsl:if test="$RIFCS_Entity_Type='Party'">
                  <xsl:call-template name="createRIFCSParty">
                     <xsl:with-param name="partytype">
                        <xsl:value-of select="$RIFCS_Entity_Sub_Type"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </xsl:if>

            </xsl:for-each>

         </xsl:element>

      </xsl:for-each>

   </xsl:template>


   <!-- mapping ohrm entity type to RIFCS type-->
   <xsl:template name="mapping_OHRM_EntityType_to_RIFCS">
      <xsl:param name="ohrmEntityType"/>
      <xsl:value-of
         select="$rifcsDoc/rifcs[OHRM_COLUMN_Value=$ohrmEntityType][1]/RIFCS_Entity"
      />
   </xsl:template>

   <!-- mapping ohrm entity subtype to RIFCS type-->
   <xsl:template name="mapping_OHRM_EntitySubType_to_RIFCS">
      <xsl:param name="ohrmEntitySubType"/>
      <xsl:value-of
         select="$rifcsDoc/rifcs[OHRM_COLUMN_Value=$ohrmEntitySubType][1]/RIFCS_Relationship"
      />
   </xsl:template>

   <!-- mapping ohrm relationship type to RIFCS type-->
   <xsl:template name="mapping_OHRM_RelationshipType_to_RIFCS">
      <xsl:param name="ohrmEntityRelationshipType"/>
      <xsl:value-of
         select="$rifcsDoc/rifcs[OHRM_COLUMN_Value=$ohrmEntityRelationshipType][1]/RIFCS_Entity"
      />
   </xsl:template>

   <!--  
        Create RIFCS activity
    -->
   <xsl:template name="createRIFCSActivity">
      <xsl:param name="activitytype"/>

      <!--<registryObject group="{$groupDescription}">-->
      <xsl:element name="registryObject" namespace="{$ns}">
         <xsl:attribute name="group">
            <xsl:value-of select="$groupDescription"/>
         </xsl:attribute>


         <xsl:element name="key">
            <xsl:value-of select="@rdf:about"/>
         </xsl:element>

         <xsl:element name="originatingSource">
            <xsl:copy-of select="$originatingSource"/>
         </xsl:element>

         <!-- TODO: allow alternatize default value if value is not in rdf -->

         <xsl:element name="activity">
            <xsl:attribute name="type">
               <xsl:copy-of select="$activitytype"/>
            </xsl:attribute>

            <xsl:call-template name="createRIFCSIdentifier"/>
            <xsl:call-template name="createRIFCSName"/>
            <xsl:call-template name="createRIFCSAddress"/>
            <xsl:call-template name="createRIFCSRelationship"/>
            <!--<xsl:call-template name="createRIFCSKeyword"/>-->
            <xsl:call-template name="createRIFCSDescription"/>
            <xsl:call-template name="createRIFCSRight"/>
         </xsl:element>
         <!-- </registryObject>-->
      </xsl:element>
   </xsl:template>

   <!--  
        Create RIFCS collection
    -->
   <xsl:template name="createRIFCSCollection">
      <xsl:param name="collectiontype"/>

      <!--   <registryObject group="{$groupDescription}">-->
      <xsl:element name="registryObject" namespace="{$ns}">
         <xsl:attribute name="group">
            <xsl:value-of select="$groupDescription"/>
         </xsl:attribute>



         <xsl:element name="key">
            <xsl:value-of select="@rdf:about"/>
         </xsl:element>

         <xsl:element name="originatingSource">
            <xsl:copy-of select="$originatingSource"/>
         </xsl:element>

         <!-- TODO: allow alternatize default value if value is not in rdf -->


         <xsl:element name="collection">
            <xsl:attribute name="type">
               <xsl:copy-of select="$collectiontype"/>
            </xsl:attribute>

            <xsl:call-template name="createRIFCSIdentifier"/>
            <xsl:call-template name="createRIFCSuriIdentifier"/>
            <xsl:call-template name="createRIFCSName"/>
            <xsl:call-template name="createRIFCSCoverage"/>
            <xsl:call-template name="createRIFCSRelationship"/>
            <!--<xsl:call-template name="createRIFCSKeyword"/>-->
            <xsl:call-template name="createRIFCSDescription"/>
            <xsl:call-template name="createRIFCSRight"/>
         </xsl:element>
         <!--</registryObject>-->
      </xsl:element>


   </xsl:template>

   <!--  
        Create RIFCS service
    -->
   <xsl:template name="createRIFCSService">
      <xsl:param name="servicetype"/>



      <xsl:element name="registryObject" namespace="{$ns}">
         <xsl:attribute name="group">
            <xsl:value-of select="$groupDescription"/>
         </xsl:attribute>



         <xsl:element name="key">
            <xsl:value-of select="@rdf:about"/>
         </xsl:element>

         <xsl:element name="originatingSource">
            <xsl:copy-of select="$originatingSource"/>
         </xsl:element>

         <!-- TODO: allow alternatize default value if value is not in rdf -->

         <xsl:element name="service">
            <xsl:attribute name="type">
               <xsl:copy-of select="$servicetype"/>
            </xsl:attribute>

            <xsl:call-template name="createRIFCSName"/>
            <xsl:call-template name="createRIFCSIdentifier"/>
            <xsl:call-template name="createRIFCSuriIdentifier"/>
            <xsl:call-template name="createRIFCSDescription"/>
            <xsl:call-template name="createRIFCSRight"/>
            <!--<xsl:call-template name="createRIFCSKeyword"/>-->
            <xsl:call-template name="createRIFCSRelationship"/>
         </xsl:element>

      </xsl:element>
   </xsl:template>

   <!--  
        Create RIFCS party
    -->
   <xsl:template name="createRIFCSParty">
      <xsl:param name="partytype"/>


      <xsl:element name="registryObject" namespace="{$ns}">
         <xsl:attribute name="group">
            <xsl:value-of select="$groupDescription"/>
         </xsl:attribute>


         <xsl:element name="key">
            <xsl:value-of select="@rdf:about"/>
         </xsl:element>

         <xsl:element name="originatingSource">
            <xsl:copy-of select="$originatingSource"/>
         </xsl:element>
         <!-- TODO: allow alternatize default value if value is not in rdf -->


         <xsl:element name="party">
            <xsl:attribute name="type">
               <xsl:copy-of select="$partytype"/>
            </xsl:attribute>

            <xsl:call-template name="createRIFCSIdentifier"/>
            <xsl:call-template name="createRIFCSuriIdentifier"/>
            <xsl:call-template name="createRIFCSName"/>
            <xsl:call-template name="createRIFCSRelationship"/>
            <!--<xsl:call-template name="createRIFCSKeyword"/>-->
            <xsl:call-template name="createRIFCSDescription"/>
            <xsl:call-template name="createRIFCSRight"/>
         </xsl:element>

      </xsl:element>
   </xsl:template>

   <!--  
        Create Local type of identifier
    -->
   <xsl:template name="createRIFCSuriIdentifier">
      <xsl:if test="./ohrm:EURL">

         <xsl:element name="identifier">
            <xsl:attribute name="type">
               <xsl:value-of select="'uri'"/>
               <!--<xsl:value-of select="'local'"/>-->
            </xsl:attribute>

            <xsl:copy-of select="./ohrm:EURL/text()"/>
         </xsl:element>

      </xsl:if>
   </xsl:template>

   <!--  
        Create primary type" Name/Title
    -->
   <xsl:template name="createRIFCSName">

      <xsl:element name="name">
         <xsl:attribute name="type">
            <xsl:value-of select="'primary'"/>
         </xsl:attribute>

         <xsl:element name="namePart">
            <xsl:copy-of select="./ohrm:ENAME/text()"/>
         </xsl:element>

      </xsl:element>

   </xsl:template>

   <!--  
        Create "long type" Description
    -->
   <xsl:template name="createRIFCSDescription">
      <xsl:if test="./ohrm:ESUMNOTE">

         <xsl:element name="description">
            <xsl:attribute name="type">
               <xsl:value-of select="'full'"/>
            </xsl:attribute>
            <xsl:copy-of select="./ohrm:ESUMNOTE/text()"/>
         </xsl:element>

      </xsl:if>
   </xsl:template>



   <!--  
        Create address
    -->
   <xsl:template name="createRIFCSAddress">
      <!--
            <xsl:call-template name="physicalAddress" />
            <xsl:call-template name="electronicAddress" />-->
   </xsl:template>

   <!--  
        Create coverage
    -->
   <xsl:template name="createRIFCSCoverage">
      <xsl:if test="./ohrm:ESTARTDATE or ./ohrm:EENDDATE">

         <xsl:element name="coverage">

            <xsl:element name="temporal">

               <xsl:if test="./ohrm:ESTARTDATE">


                  <xsl:element name="date">
                     <xsl:attribute name="type">
                        <xsl:value-of select="'dateFrom'"/>
                     </xsl:attribute>
                     <xsl:attribute name="dateFormat">
                        <xsl:value-of select="'W3C'"/>
                     </xsl:attribute>
                     <xsl:copy-of select="./ohrm:ESTARTDATE/text()"/>
                  </xsl:element>

               </xsl:if>
               <xsl:if test="./ohrm:EENDDATE">


                  <xsl:element name="date">
                     <xsl:attribute name="type">
                        <xsl:value-of select="'dateTo'"/>
                     </xsl:attribute>
                     <xsl:attribute name="dateFormat">
                        <xsl:value-of select="'W3C'"/>
                     </xsl:attribute>
                     <xsl:copy-of select="./ohrm:EENDDATE/text()"/>
                  </xsl:element>

               </xsl:if>

            </xsl:element>
         </xsl:element>

      </xsl:if>
   </xsl:template>

   <!--  
        Create description 
    -->
   <xsl:template name="createRIFCSRight">
      <!--
            <xsl:apply-templates select="dcterms:rights" />
        -->
      <xsl:if test="./ohrm:EREFERENCE">

         <xsl:element name="description">
            <xsl:attribute name="type">
               <xsl:value-of select="'rights'"/>
            </xsl:attribute>
            <xsl:copy-of select="./ohrm:EREFERENCE/text()"/>
         </xsl:element>

      </xsl:if>
   </xsl:template>


   <!--  
        Create keyword 
    -->
   <xsl:template name="createRIFCSKeyword">

      <!--
            <xsl:apply-templates select="vivo:hasSubjectArea" />-->
      <xsl:if test="./ohrm:EFUNCTION">

         <xsl:element name="subject">
            <xsl:attribute name="type">
               <xsl:value-of select="'local'"/>
            </xsl:attribute>
            <xsl:copy-of select="./ohrm:EFUNCTION/text()"/>
         </xsl:element>

      </xsl:if>
   </xsl:template>

   <!--  
        Create URL Identifier 
    -->
   <xsl:template name="createRIFCSIdentifier">

      <!-- every OHRM element has at least 1 local ID -->
      <xsl:element name="identifier">
         <xsl:attribute name="type">
            <xsl:value-of select="'local'"/>
         </xsl:attribute>
         <xsl:value-of select="@rdf:about"/>
      </xsl:element>


      <!-- OHRM elements may also have alternate names acting as additional identifiers -->
      <xsl:if test="./ohrm:hasEntityName">

         <xsl:variable name="allohrmEntityName"
            select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='ENTITYNAME' and OHRM_TABLE_COLUMN='ENALTERNATE' ]/rifcs/OHRM_COLUMN_Value"/>
         <xsl:variable name="select_all_OHRM_EntityName_instances"
            select="*[local-name()='hasEntityName']"/>

         <xsl:for-each select="$select_all_OHRM_EntityName_instances">

            <!--  <xsl:variable name="referredEntityId" select="$select_all_OHRM_EntityName_instances/current()/@rdf:resource"/>
             -->

            <xsl:variable name="referredEntityId"
               select="$select_all_OHRM_EntityName_instances/./@rdf:resource"/>
            <xsl:variable name="referredEntityType"
               select="ancestor::rdf:RDF/rdf:Description[@rdf:about=$referredEntityId]/ohrm:ENALTERNATETYPE/text()"/>
            <xsl:variable name="referredEntityText"
               select="ancestor::rdf:RDF/rdf:Description[@rdf:about=$referredEntityId]/ohrm:ENALTERNATE/text()"/>

            <xsl:for-each select="$select_all_OHRM_EntityName_instances">

               <xsl:for-each select="$allohrmEntityName">

                  <xsl:if
                     test="$allohrmEntityName/*[name()=name(current())]/text()=$referredEntityType">
                     <xsl:variable name="related_rifcs_Entity_name"
                        select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='ENTITYNAME' and OHRM_TABLE_COLUMN='ENALTERNATE' ]/rifcs/OHRM_COLUMN_Value[text()=$referredEntityType]/preceding-sibling::RIFCS_Relationship/text()"/>


                     <xsl:element name="identifier">
                        <xsl:attribute name="type">
                           <xsl:value-of select="$related_rifcs_Entity_name"/>
                        </xsl:attribute>
                        <xsl:copy-of select="$referredEntityText"/>
                     </xsl:element>


                  </xsl:if>

               </xsl:for-each>

            </xsl:for-each>
         </xsl:for-each>

      </xsl:if>
   </xsl:template>


   <!-- create relationship -->
   <xsl:template name="createRIFCSRelationship">
      <!--
<test123>
    <xsl:copy-of select="."></xsl:copy-of>
    </test123>
-->
      <xsl:variable name="select_all_OHRM_instances" select="*"/>
      <xsl:variable name="allohrmRelationshipName"
         select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='RELATEDENTITY' and OHRM_TABLE_COLUMN='TYPE' ]/OHRM_COLUMN_Value"/>
      <!--
        <test234>
    <xsl:copy-of select="$allohrmRelationshipName"></xsl:copy-of>
    </test234>
-->

      <xsl:for-each select="$allohrmRelationshipName/.">

         <xsl:variable name="current_ohrm_Relationship_name"
            select="$allohrmRelationshipName[text()=current()]"/>
         <!--
<test345>
    <xsl:copy-of select="$current_ohrm_Relationship_name"></xsl:copy-of>
    </test345>
-->
         <xsl:for-each
            select="($select_all_OHRM_instances[local-name()=$current_ohrm_Relationship_name])">

            <xsl:element name="relatedObject">

               <xsl:element name="key">
                  <xsl:value-of select="@rdf:resource"/>
               </xsl:element>



               <!--   <xsl:variable name="referred_rifcs_Relationship_name" select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='RELATEDENTITY' and OHRM_TABLE_COLUMN='TYPE' ]/OHRM_COLUMN_Value[text()=$current_ohrm_Relationship_name]/./RIFCS_Relationship/text()"/>
                  
                    parent::node()
                    -->
               <xsl:variable name="referred_rifcs_Relationship_name"
                  select="document('rifcs-mapping.xml')/dataroot/rifcs[OHRM_TABLE='RELATEDENTITY' and OHRM_TABLE_COLUMN='TYPE' ]/OHRM_COLUMN_Value[text()=$current_ohrm_Relationship_name]/preceding-sibling::RIFCS_Relationship/text()"/>
               <!--
<test456>
    <xsl:copy-of select="$referred_rifcs_Relationship_name"></xsl:copy-of>
    </test456>
-->
               <xsl:element name="relation">
                  <xsl:attribute name="type">
                     <xsl:value-of select="$referred_rifcs_Relationship_name"/>
                  </xsl:attribute>
               </xsl:element>
            </xsl:element>



         </xsl:for-each>

      </xsl:for-each>


      <!--
            <xsl:for-each select="./ohrm:isManagedBy">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isManagedBy"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:describes">
            <relatedObject>
            <key>
            <xsl:copy-of  select="@rdf:resource"/>  
            </key>
            <relation type="describes"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isDescribedBy">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isDescribedBy"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:enriches">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="enriches"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isEnrichedBy">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isEnrichedBy"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:hasPart">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="hasPart"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isLocatedIn">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isLocatedIn"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isLocationFor">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isLocationFor"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isCollectorOf">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isCollectorOf"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:hasCollector">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="hasCollector"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isOutputOf">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isOutputOf"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:hasOutput">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="hasOutput"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:hasParticipant">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="hasParticipant"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:hasAssociationWith">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="hasAssociationWith"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isFundedBy">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isFundedBy"/>
            </relatedObject>
            </xsl:for-each>
            <xsl:for-each select="./ohrm:isFunderOf">
            <relatedObject>
            <key>
            <xsl:copy-of select="@rdf:resource"/>  
            </key>
            <relation type="isFunderOf"/>
            </relatedObject>
            </xsl:for-each>
            
        -->
      <!--reverse relationship -->

   </xsl:template>

   <xsl:template name="partyname">
      <xsl:apply-templates select="name"/>
      <xsl:apply-templates select="title"/>
      <xsl:apply-templates select="givenName"/>
      <xsl:apply-templates select="familyName"/>
      <xsl:apply-templates select="suffixName"/>
   </xsl:template>
   <xsl:template match="name">

      <xsl:element name="namePart">
         <xsl:value-of select="."/>
      </xsl:element>

   </xsl:template>
   <xsl:template match="title">

      <xsl:element name="namePart">
         <xsl:value-of select="."/>
      </xsl:element>

   </xsl:template>
   <xsl:template match="givenName">

      <xsl:element name="namePart">
         <xsl:attribute name="type">
            <xsl:value-of select="'given'"/>
         </xsl:attribute>
         <xsl:value-of select="."/>
      </xsl:element>

   </xsl:template>
   <xsl:template match="familyName">
      <xsl:element name="namePart">
         <xsl:attribute name="type">
            <xsl:value-of select="'family'"/>
         </xsl:attribute>
         <xsl:value-of select="."/>
      </xsl:element>

   </xsl:template>
   <xsl:template match="suffixName">
      <xsl:element name="namePart">
         <xsl:attribute name="type">
            <xsl:value-of select="'suffix'"/>
         </xsl:attribute>
         <xsl:value-of select="."/>
      </xsl:element>
   </xsl:template>







</xsl:stylesheet>
